package cn.bupt.adt.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.bupt.adt.dao.autogenerate.InspectOrderViewMapper;
import cn.bupt.adt.dao.autogenerate.InstitutionAppUserMapper;
import cn.bupt.adt.dao.autogenerate.InstitutionViewMapper;
import cn.bupt.adt.entity.autogenerate.InstitutionAppUser;
import cn.bupt.adt.entity.autogenerate.InstitutionView;
import cn.bupt.adt.entity.autogenerate.InstitutionViewExample;
import cn.bupt.adt.service.IInstitutionAppUserService;
import cn.bupt.adt.util.MD5Util;

@Service
public class InstitutionAppUserService implements IInstitutionAppUserService {

	@Autowired
	InstitutionViewMapper institutionViewMapper;
	
	@Autowired
	InspectOrderViewMapper inspectOrderViewMapper;
	
	@Autowired
	InstitutionAppUserMapper institutionAppUserMapper;
	
	@Override
	public InstitutionView getInstitutionByAccount(String account) {
		InstitutionViewExample ex = new InstitutionViewExample();
		ex.createCriteria().andAccountEqualTo(account);
		List<InstitutionView> lists = institutionViewMapper.selectByExample(ex);
		return lists.size() == 1?lists.get(0):null;
	}

	@Override
	public void updateLoginTimeById(Integer id, Date loginTime) {
		InstitutionAppUser user = institutionAppUserMapper.selectByPrimaryKey(id);
		if(user != null) {
			user.setLastLoginTime(user.getLoginTime());
			user.setLoginTime(loginTime);
			institutionAppUserMapper.updateByPrimaryKeySelective(user);
		}
	}

	@Override
	public InstitutionAppUser getInstitutionById(Integer userId) {
		return institutionAppUserMapper.selectByPrimaryKey(userId);
	}

	@Override
	public boolean changePasswd(InstitutionAppUser ins, String newPasswd) {
		ins.setPasswd(MD5Util.MD5(newPasswd));
		return institutionAppUserMapper.updateByPrimaryKeySelective(ins) == 1;
	}

	@Override
	public void updateInstitutionAppUser(InstitutionAppUser ins) {
		institutionAppUserMapper.updateByPrimaryKeySelective(ins);
	}

	@Override
	public InstitutionView getInstitutionViewById(Integer userId) {
		InstitutionViewExample example = new InstitutionViewExample();
		example.createCriteria().andIdEqualTo(userId);
		List<InstitutionView> list = institutionViewMapper.selectByExample(example);
		return list.size() == 1?list.get(0):null;
	}

	
}
